// 3ds max effect file
// Simple vertex color - work with the Vertex Paint tool.  The max effect parser
// allows you to define any arbitary map channel to be passed in via a texcoord.
// In this case we are interested in Vertex Color, Illumination and Alpha which 
// are stored in 0,-1,-2 respectively.



// light direction (view space)

// transformations
float4x4 World      : 		WORLD;
float4x4 View       : 		VIEW;
float4x4 Projection : 		PROJECTION;
float4x4 WorldViewProj : 	WORLDVIEWPROJ;
float4x4 WorldView : 		WORLDVIEW;

texture normalMap : NormalMap < 
	string name = "NMP_Ripples2_512.dds"; 
	string UIName = "Normal Texture";
	>;
    
sampler2D normalSampler = sampler_state 
{
	Texture = <normalMap>;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
};

int texcoord0 : Texcoord
<
    int Texcoord = 0;
    int MapChannel = 1;
>;

int texcoord1 : Texcoord
<
    int Texcoord = 1;
    int MapChannel = 2;
>;

int texcoord2 : Texcoord
<
    int Texcoord = 2;
    int MapChannel = 3;
>;

int texcoord3 : Texcoord
<
    int Texcoord = 3;
    int MapChannel = 4;
>;

struct AppData
{
    float3 Pos      : POSITION; 
    float3 Norm     : NORMAL;
    float2 UV       : TEXCOORD0;
    float3 Color0   : TEXCOORD1;
    float3 Color1   : TEXCOORD2;
    float3 Color2   : TEXCOORD3;
};

struct VS_OUTPUT
{
    float4 Pos      : POSITION;
    float2 UV       : TEXCOORD0;
    float4 Color0   : TEXCOORD1;
    float4 Color1   : TEXCOORD2;
    float4 Color2   : TEXCOORD3;
};

VS_OUTPUT VS(
    AppData IN
)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;
    
    Out.Pos = mul( float4(IN.Pos,1), WorldViewProj );    // position (projected)
    
    Out.UV = IN.UV;
    
    Out.Color0.rgb = IN.Color0.xyz * float3( 1,-1, 1);
    Out.Color0.a = 1;
    
    Out.Color1.rgb = IN.Color1.xyz * float3( 1,-1, 1);
    Out.Color1.a = 1;
    
    Out.Color2.rgb = IN.Color2.xyz * float3( 1,-1, 1);
    Out.Color2.a = 1;
    
    return Out;
    
}

float4 PS(
    VS_OUTPUT IN
) : COLOR0
{
    //float3 n = float3( 0, 0, 1 );
    
    float3 n = tex2D( normalSampler, IN.UV ).xyz * 2 - 1;
    
    n = normalize( n );
    
    float3 b0 = normalize( float3(-0.40824829046386307, 0.70710678118654746, 0.57735026918962584) );
    float3 b1 = normalize( float3(-0.40824829046386307, -0.70710678118654746, 0.57735026918962584) );
    float3 b2 = normalize( float3(0.81649658092772615, 0, 0.57735026918962584) );
    
    float3 w;
    
    w.x = saturate( dot( n, b0 ) );
    w.y = saturate( dot( n, b1 ) );
    w.z = saturate( dot( n, b2 ) );
    
    //w = normalize( w );
    w *= w;
    w /= dot( w, float3( 1, 1, 1) );
    
    float4 OUT = 
          IN.Color0 * w.x
        + IN.Color1 * w.y
        + IN.Color2 * w.z
        ;
    
    return OUT;
    
}

technique RNMOnly
{
    pass P0
    {
        ZEnable             = true;
        ZWriteEnable        = true;
        //AlphaBlendEnable    = TRUE;
        //SrcBlend            = SRCALPHA;
        //DestBlend           = InvSrcAlpha;  
        //CullMode            = None;
        
        // shaders
        VertexShader = compile vs_2_0 VS();
        PixelShader = compile ps_2_0 PS();
    }
}